#!/usr/bin/env python

import sys, os
from course_class import Course
from liwe.decorators import dataset

#{{{def _create_dataset ( start_point, lines, rows, tot_rows ):
def _create_dataset ( start_point, lines, rows, tot_rows ):
	res = {}

	count = 0
	for v in rows:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v
		count += 1

	res [ 'rows' ] = tot_rows
	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = lines

	return res
#}}}
def get_templates ( liwe ):
	n = Course ( liwe )
	return { "templates" : n.get_templates () }


def get_info ( liwe ):
	uid = liwe.pyhp.session.data.get ( "uid", -1 )
	n = Course ( liwe ) 
	return n.list_user ( uid = uid )
	

#{{{def get_course ( liwe, id_course = 0 ):
def get_course ( liwe, id_course = 0 ):
	"""
	:INPUT:
                - id_course:			SAFE_NUMBER	
	"""
	n = Course ( liwe )
	return { "course" : n.get_course ( id_course = id_course ) }
#}}}
#{{{def list_course ( liwe, name = '', descr = '', visible = 0, day_duration = 0, tags = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
#def list_course ( liwe, name = '', descr = '', visible = 0, day_duration = 0, tags = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
#		- quant:		SAFE_NUMBER
#		- _X_LINES:		SAFE_NUMBER
#		- _X_START_POINT:	SAFE_NUMBER
#		- _X_PAGE:		SAFE_NUMBER
@dataset
def list_course ( liwe, name = '', descr = '', visible = -1, day_duration = 0, nodates = 0, validdates = 0, tags = '', limit = 10, start_point = 0, incoming = 0 ):
	"""
	:INPUT:
		- name:			SAFE_DB_STRING	
		- descr:		SAFE_DB_STRING	
		- visible:		SAFE_NUMBER	
		- day_duration:		SAFE_NUMBER	
		- limit:		SAFE_NUMBER	
		- nodates:		SAFE_NUMBER	
		- validdates:		SAFE_NUMBER	
		- incoming:		SAFE_NUMBER	
		- start_point:		SAFE_NUMBER	
		- tags:			SAFE_DB_STRING	
	"""

	n = Course ( liwe )
	resp = n.list_course ( name = name, descr = descr, visible = visible, day_duration = day_duration, nodates = nodates,
						   validdates = validdates, tags = tags, quant = limit, start_point = start_point, do_count = 1, incoming =  incoming )

	return resp
#}}}
def list_course_plain ( liwe, name = '', descr = '', visible = 0, day_duration = 0, tags = '' ):
	n = Course ( liwe )
	resp = n.list_course ( name = name, descr = descr, visible = visible, day_duration = day_duration,  tags = tags )
	rows = resp [ 'rows' ]
	return { 'course': rows }


#{{{def get_charge ( liwe, id_charge = 0 ):
def get_charge ( liwe, id_charge = 0 ):
	"""
	:INPUT:
                - id_charge:			SAFE_NUMBER	
	"""
	n = Course ( liwe )
	return { "charge" : n.get_charge ( id_charge = id_charge ) }
#}}}
#{{{def list_charge ( liwe, id_course = 0, descr = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_charge ( liwe, id_course = 0, descr = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):

	"""
	:INPUT:
                - id_course:		SAFE_NUMBER	
                - descr:		SAFE_DB_STRING	
		- quant:		SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	"""

	use_dataset = 0

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	n = Course ( liwe )
	resp = n.list_charge ( id_course = id_course, descr = descr, quant = quant, start_point = start_point, do_count = use_dataset )


	rows = resp [ 'rows' ]
	tot_rows =  resp [ 'tot_rows' ]

	if not use_dataset: 
		return { 'charge': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}
#{{{def get_calendar ( liwe, id_calendar = 0 ):
def get_calendar ( liwe, id_calendar = 0 ):
	"""
	:INPUT:
                - id_calendar:			SAFE_NUMBER	
	"""
	n = Course ( liwe )
	return { "calendar" : n.get_calendar ( id_calendar = id_calendar ) }
#}}}
#{{{def list_calendar ( liwe, mode = '',  id_course = 0, start_date_from = '', start_date_to = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_calendar ( liwe, mode = '',id_course = 0, start_date_from = '', start_date_to = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):

	"""
	:INPUT:
		- mode:			SAFE_STRING
                - id_course:		SAFE_NUMBER	
                - start_date_from:	SAFE_DB_STRING	
                - start_date_to:	SAFE_DB_STRING	
		- quant:		SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	"""

	use_dataset = 0

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	n = Course ( liwe )
	resp = n.list_calendar ( mode = mode, id_course = id_course, start_date_from = start_date_from, start_date_to = start_date_to, quant = quant, start_point = start_point, do_count = use_dataset )


	rows = resp [ 'rows' ]
	tot_rows =  resp [ 'tot_rows' ]

	if not use_dataset: 
		return { 'calendar': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}
def incoming_dates ( liwe , how_many ):
	"""
	:INPUT:
		- how_many: SAFE_NUMBER
	"""
	n = Course ( liwe )
	res = n.list_calendar ( mode = "incoming", quant = how_many, start_point = 0, do_count = 1 )
	
	return { 'upcoming' : res [ 'rows'] }



#{{{def get_user ( liwe, uid = 0, id_calendar = 0 , id_course = 0 ):
def get_user ( liwe, id_user = 0 ):
	"""
	:INPUT:
                - id_user:			SAFE_NUMBER	
	"""
	n = Course ( liwe )
	return { "user" : n.get_user ( id_user = id_user ) }
#}}}
#{{{def list_user ( liwe, uid = 0, id_course = 0, id_calendar = 0, date_from = '', date_to = '', description = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_user ( liwe, uid = 0, id_course = 0, id_calendar = 0, date_from = '', date_to = '', description = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):

	"""
	:INPUT:
		- uid:			SAFE_NUMBER	
		- id_course:		SAFE_NUMBER	
		- id_calendar:		SAFE_NUMBER	
		- date_from:		SAFE_DB_STRING	
		- date_to:		SAFE_DB_STRING	
		- description:		SAFE_DB_STRING	
		- quant:		SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	"""

	use_dataset = 0

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	n = Course ( liwe )
	resp = n.list_user (uid = uid, id_calendar = id_calendar, id_course = id_course, date_from = date_from, date_to = date_to, description = description,  quant = quant, start_point = start_point, do_count = use_dataset )


	rows = resp [ 'rows' ]
	tot_rows =  resp [ 'tot_rows' ]

	if not use_dataset: 
		return { 'profile': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}
def get_user_charge ( liwe, id_course = 0, id_calendar = 0 ):
	"""
	:INPUT:
		- id_course:			SAFE_NUMBER	
		- id_calendar:			SAFE_NUMBER	
	"""
	n = Course ( liwe )
	res = n.get_user_charge ( id_course =  id_course, id_calendar = id_calendar )
	return { 'charge' : res }

def show_register ( liwe, id_calendar = 0 ):
	"""
	:INPUT:
		- id_calendar:			SAFE_NUMBER	
	"""
	if not id_calendar : return { 'course' : [] }
	n =  Course ( liwe )

	calendar =  n.get_calendar ( id_calendar =  id_calendar )
	calendar [ 'id_calendar' ] = calendar [ 'id' ]
	del calendar [ 'id' ]
	if not calendar [ 'city' ] : del calendar  [ 'city' ]
	if not calendar [ 'address' ] : del calendar  [ 'address' ]
	
	course = n.get_course ( id_course =  calendar [ 'id_course' ] )
	
	
	course [ 'descrizione_corso' ] = course [ 'descr' ]
	course.update ( calendar )

	charge = n.get_user_charge ( id_calendar = id_calendar, id_course = calendar [ 'id_course' ] )
	course.update ( charge )

	members = n.dbm.single_row	( "course_calendar_view", [ 'members' ], "id_course = %s and id=%s" % ( calendar [ 'id_course' ], id_calendar ) );
	course.update ( members )

	return { 'course' : course }	

#{{{def set_user ( liwe,id_user = 0, id_calendar = 0, id_course = 0, subscription_date = '', note = '', description = '' ):
def set_user ( liwe, id_calendar = 0, id_course = 0, subscription_date = '', note = '', send_mail = ''):
        """     
        :INPUT:        	 
        - id_calendar:		SAFE_NUMBER       
        - id_course:		SAFE_NUMBER       
        - subscription_date:	SAFE_DATE	
        - note:			SAFE_DB_STRING
	- send_mail:		SAFE_DB_STRING
	"""
	n = Course ( liwe )


	#Get user in session
	uid = liwe.pyhp.session.data.get ( "uid", -1 )
	if uid == -1 : return []


	resp = n.set_user ( uid = uid, id_calendar = id_calendar,
			    id_course = id_course, subscription_date = subscription_date,
			    note = note, send_mail = send_mail )

	return { 'user' : resp }

#}}}

def paypal_cback ( liwe, kwargs = {} ):
	"""
	:INPUT:
	"""
	n = Course ( liwe )
	n.paypal_cback ( kwargs )


def session_set_item ( liwe, key = '' ,  coupon = '', id_course = 0, id_calendar = 0 ):
	"""
	:INPUT:
		- key 				SAFE_STRING
		- id_course:			SAFE_NUMBER	
		- id_calendar:			SAFE_NUMBER	
		- coupon:			SAFE_STRING
	"""
	uid = liwe.pyhp.session.data.get ( "uid", -1 )
	if not uid : return
	liwe.invoke ( "transaction.ajax.custom_add", module = "course", key = "cback", values = "course.ajax.paypal_cback" )
	liwe.invoke ( "transaction.ajax.custom_add", module = "course", key = "uid", values = uid )
	liwe.invoke ( "transaction.ajax.custom_add", module = "course", key = "courses", values = dict ( id_course = id_course, id_calendar = id_calendar, coupon = coupon ), is_list =  True )


def session_del_item ( liwe, key ):
	"""
	:INPUT:
		- key 				SAFE_STRING
	"""
	import sys
	import json
	liwe.invoke ( "transaction.ajax.custom_del", module = "course", key = key )
	#Check
	custom = liwe.invoke ( "transaction.ajax.custom_serialize" ) [ 'custom' ]
	custom = json.loads ( custom )
	if ( custom [ 'course' ].keys() == [ 'cback' ] ):
		liwe.invoke ( "transaction.ajax.custom_del_module", module = 'course' ) 
		
def session_del_all ( liwe ):
	liwe.invoke ( "transaction.ajax.custom_del_module", "course" )

def ready_to_checkout ( liwe ):
	"""
	:INPUT:
		- id_course		SAFE_NUMBER
	"""
	user = liwe.invoke ( "user.ajax.get_info" )
	if not user.get ( "uid" ):
		return  { "status" : "login" }
	
	profile = liwe.invoke ( "address_book.ajax.get_info", 1 )

	if profile [ 'err_code' ]:
		return { "status" : "profile" }
	
	if not  profile [ 'profile' ] [ 'address' ].get ( 'tot_rows' ):
		return { "status" : "address", "profile" : profile }

	return { "status" : "ready" , "profile" : profile }
		
	
	







	
	

